import { Controller, Get, Post, Body, Patch, Param, Delete, Put, Request, HttpCode, Query, Res } from '@nestjs/common';
import { DictService } from './dict.service';
import { CreateDictDataDto, CreateDictTypeDto, ListDictData, ListDictType, UpdateDictDataDto, UpdateDictTypeDto } from './dto/index.dto';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';

@ApiTags('字典管理')
@ApiBearerAuth()
@Controller('system/dict')
export class DictController {
  constructor(private readonly dictService: DictService) { }
  //字典类型
  @ApiOperation({
    summary: '字典类型-创建'
  })
  @ApiBody({
    type: CreateDictTypeDto,
    required: true
  })
  @HttpCode(200)
  @Post('/type/create')
  createType(@Body() info: CreateDictTypeDto, @Request() req) {
    return this.dictService.createType(info);
  }

  @ApiOperation({
    summary: "字典类型-删除"
  })
  @HttpCode(200)
  @Delete('/type/delete')
  removeType(@Body() info: { ids: number[] }) {
    return this.dictService.removeType(info)
  }

  @ApiOperation({
    summary: '字典类型-更新'
  })
  @ApiBody({
    type: UpdateDictTypeDto,
    required: true
  })
  @HttpCode(200)
  @Put('/type/update')
  updateType(@Body() info: UpdateDictTypeDto, @Request() req) {
    return this.dictService.updateType(info)
  }

  @ApiOperation({
    summary: '字典类型-列表'
  })
  @ApiBody({
    type: ListDictType,
    required: true
  })
  @Get('/type/list')
  queryTypeList(@Query() query: ListDictType) {
    return this.dictService.queryTypeList(query)
  }

  //字典数据
  @ApiOperation({
    summary: '字典数据-创建'
  })
  @ApiBody({
    type: CreateDictDataDto,
    required: true
  })
  @HttpCode(200)
  @Post('/data/create')
  createData(@Body() info: CreateDictDataDto, @Request() req) {
    return this.dictService.createData(info)
  }

  @ApiOperation({
    summary: '字典数据-删除'
  })
  @HttpCode(200)
  @Delete('/data/delete')
  removeData(@Body() info: { ids: number[] }) {
    return this.dictService.removeData(info)
  }

  @ApiOperation({
    summary: '字典数据-更新'
  })
  @ApiBody({
    type: UpdateDictDataDto,
    required: true
  })
  @HttpCode(200)
  @Put('/data/update')
  updateData(@Body() info: UpdateDictDataDto, @Request() req) {
    return this.dictService.updateData(info)
  }

  @ApiOperation({
    summary: '字典数据-列表'
  })
  @ApiBody({
    type: ListDictData,
    required: true
  })
  @HttpCode(200)
  @Get('/data/list')
  queryDataList(@Query() query: ListDictData) {
    return this.dictService.queryDataList(query);
  }

  @ApiOperation({
    summary: '根据字典类型查询所有字典值'
  })
  @Get('/data/all')
  queryDataAll(@Query() query: { code: string,status?: number; }) {
    return this.dictService.queryDataAll(query);
  }

  @ApiOperation({
    summary: '字典数据导出'
  })
  @Get('/type/export')
  async export(@Res() res: Response, @Body() body: ListDictType): Promise<void> {
    return this.dictService.export(res, body)
  }
}
